package sky.wicket;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Ticket;
import sky.service.TicketService;
import sky.tro.TicketFinder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 31.08.13
 * Time: 19:15
 * To change this template use File | Settings | File Templates.
 */
public class Personals extends BasePage {

    @SpringBean
    private TicketService ticketService;

    public Personals() {
        super();
    }

    public Personals(TicketFinder ticketFinder, final Integer flightId) {
        super();
        List<Ticket> tickets = new ArrayList<Ticket>(ticketFinder.getTicketCount());
        for (int i = 0; i<ticketFinder.getTicketCount(); i++) {
            tickets.add(new Ticket());
        }
        System.out.println(ticketFinder.getTicketCount());
        Form form = new Form("form");
        ListView<Ticket> listView = new ListView<Ticket>("tickets", tickets) {
            @Override
            protected void populateItem(ListItem<Ticket> components) {
                final Ticket ticket = components.getModelObject();
                components.add(new TextField<String>("user.firstName", new PropertyModel<String>(ticket, "user.firstName")));
                components.add(new TextField<String>("user.lastName", new PropertyModel<String>(ticket, "user.lastName")));
                components.add(new TextField<Integer>("user.telephone", new PropertyModel<Integer>(ticket, "user.telephone")));
                components.add(new CheckBox("child", new PropertyModel<Boolean>(ticket, "user.child")));
                final DropDownChoice<Integer> rows = new DropDownChoice<Integer>("row", new PropertyModel<Integer>(ticket, "row"),
                        ticketService.getFreeRows(flightId));
                for (Integer i: ticketService.getFreeRows(flightId)) {
                    System.out.println(i + " ");
                }
                final DropDownChoice<Character> places = new DropDownChoice<Character>("place", new PropertyModel<Character>(ticket, "place"),
                        ticketService.getFreePlaces(flightId));
                components.add(rows);
                components.add(places);
                rows.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                    @Override
                    protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
                        places.setChoices(ticketService.getFreePlacesOnRow(flightId, rows.getModelObject()));
                        if (ajaxRequestTarget != null) {
                            ajaxRequestTarget.add(places);
                        }
                    }
                });
                places.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                    @Override
                    protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
                        rows.setChoices(ticketService.getFreeRowsOnPlace(flightId, places.getModelObject()));
                        if (ajaxRequestTarget != null) {
                            ajaxRequestTarget.add(rows);
                        }
                    }
                });
            }
        };
        form.add(listView);
        add(form);
    }
}
